package com.wanfajie.smsdistribute.db.bean.phone

import com.j256.ormlite.dao.Dao
import com.j256.ormlite.dao.ForeignCollection
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.field.ForeignCollectionField
import com.j256.ormlite.misc.BaseDaoEnabled
import com.j256.ormlite.table.DatabaseTable
import com.wanfajie.smsdistribute.MyApplication
import com.wanfajie.smsdistribute.R
import com.wanfajie.smsdistribute.db.helper.PhoneDatabaseHelper
import com.wanfajie.smsdistribute.db.quietInvoke
import java.sql.SQLException

@DatabaseTable(tableName = "sd_phone_number")
data class PhoneNumber(

        @DatabaseField(columnName = ID, generatedId = true, readOnly = true)
        var id : Int? = null,

        @DatabaseField(columnName = NAME_FILED, canBeNull = false)
        var name : String = "",

        @DatabaseField(columnName = NUMBER_FIELD, canBeNull = false)
        private var number : String = "",

        @DatabaseField(columnName = GROUP_ID, foreign = true, canBeNull = false)
        val group: PhoneGroup? = null
): BaseDaoEnabled<PhoneNumber, Int?>() {
    companion object {
        const val ID = "id"
        const val NUMBER_FIELD = "tel_no"
        const val NAME_FILED = "name"
        const val GROUP_ID = "group_id"

        val DAO: Dao<PhoneNumber, Int?> = PhoneDatabaseHelper.getDao(PhoneNumber::class.java)

        fun create(name: String, number: String, group: PhoneGroup): PhoneNumber? {
            val phoneNumber = PhoneNumber(name = name, number = number, group = group)
            return quietInvoke(PhoneNumber::class.java.simpleName, null, SQLException::class) {
                phoneNumber.create()
                phoneNumber
            }
        }
    }

    @Deprecated("DO NOT do anything to this field")
    @DatabaseField(columnName = "${GROUP_ID}_$NUMBER_FIELD", unique = true, canBeNull = false)
    var uniqueField = if (group != null) "${group.id}_$number" else ""

    @ForeignCollectionField(eager = true)
    var values: ForeignCollection<PhoneFieldValue>? = null

    init {
        dao = DAO
    }

    fun getNumber(): String = number

    fun updateNumber(newNumber: String): Boolean {
        if (group == null) throw IllegalStateException("未分组记录无法修改电话号码: $this")

        number = newNumber.replace(" ", "")
        uniqueField = "${group.id}_$number"

        return quietInvoke(javaClass.simpleName, false, SQLException::class) {
            update() > 0
        }
    }
    fun set(key: String, value: String) {
        if (values == null)
            values = dao.getEmptyForeignCollection(PhoneFieldValue.PHONE_ID)

        val valueCollect = values!!
        var fieldValue = valueCollect.firstOrNull { it.key == key }
        if (fieldValue == null) {
            val keyField = PhoneKeyField.get(key)
            fieldValue = PhoneFieldValue(this, keyField, value)
            valueCollect.add(fieldValue)
        } else {
            fieldValue.value = value
            valueCollect.update(fieldValue)
        }
    }

    fun get(key: CharSequence): String {
        return when (key) {
            MyApplication.context.getString(R.string.person_name) -> name
            MyApplication.context.getString(R.string.tel) -> number
            else -> {
                values?.firstOrNull {
                    it.key == key
                }?.value ?: ""
            }
        }
    }
}